package com.atguigu.gmall.sms.service.impl;

import com.atguigu.gmall.sms.entity.SkuFullReductionEntity;
import com.atguigu.gmall.sms.entity.SkuLadderEntity;
import com.atguigu.gmall.sms.service.SkuFullReductionService;
import com.atguigu.gmall.sms.service.SkuLadderService;
import com.atguigu.gmall.sms.vo.SkuSaleInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.sms.mapper.SkuBoundsMapper;
import com.atguigu.gmall.sms.entity.SkuBoundsEntity;
import com.atguigu.gmall.sms.service.SkuBoundsService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuBoundsService")
public class SkuBoundsServiceImpl extends ServiceImpl<SkuBoundsMapper, SkuBoundsEntity> implements SkuBoundsService {

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuBoundsEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuBoundsEntity>()
        );

        return new PageResultVo(page);
    }


    @Autowired
    private SkuFullReductionService skuFullReductionService;


    @Autowired
    private SkuLadderService skuLadderService;


    @Override
    @Transactional(rollbackFor = ArithmeticException.class)
    public void saveSkuSaleInfo(SkuSaleInfoVo skuSaleInfoVo) {
        SkuBoundsEntity skuBoundsEntity = new SkuBoundsEntity();
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();

        //1.sku_bounds表,保存积分数据
        BeanUtils.copyProperties(skuSaleInfoVo, skuBoundsEntity);
        Integer[] works = skuSaleInfoVo.getWork();
        skuBoundsEntity.setWork(works[0] * 8 + works[1] * 4 + works[2] * 2 + works[3] * 1);
        this.save(skuBoundsEntity);

        //2.sku_full_reduction表，保存满减数据
        BeanUtils.copyProperties(skuSaleInfoVo, skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(skuSaleInfoVo.getFullAddOther());
        skuFullReductionService.save(skuFullReductionEntity);

        //3.sku_ladder,保存折扣数据
        BeanUtils.copyProperties(skuSaleInfoVo, skuLadderEntity);
        skuLadderEntity.setAddOther(skuSaleInfoVo.getLadderAddOther());
        skuLadderService.save(skuLadderEntity);

        //用来测试分布式事务的，注掉
        //int i = 1/0;
    }

}